package com.rc.activity.sync.util;

import java.util.ArrayList;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.AsyncTask;

import com.rc.activity.dao.DaoSession;
import com.rc.activity.dao.tbl_Merchant;
import com.rc.activity.dao.tbl_MerchantDao;
import com.rc.net.NetSupport;

public class MerchantSync {

	public static String[] keys = { "status", "MerchantID", "messages" };

	private Context context;
	private SQLiteDatabase db;
	private DaoSession daoSession;
	public String query;
	private tbl_MerchantDao daoMerchant;
	public Cursor cursor;

	public MerchantSync(Context context, SQLiteDatabase db,
			DaoSession daoSession) {
		this.context = context;
		this.daoSession = daoSession;
		this.db = db;
		daoMerchant = this.daoSession.getTbl_MerchantDao();
	}

	/**
	 * Delete multi Merchant
	 */
	public void deleteMerchant() {
		// new callDeleteMerchant().execute("");
	}

	/**
	 * Insert Merchant
	 */
	public void insertMerchant() {
		new callInsertMerchant().execute("");
	}

	/**
	 * Update Merchant
	 */
	public void updateMerchant() {
		new callUpdateMerchant().execute("");
	}

	/**
	 * TODO : call multidelteMerchant unused
	 */
	/*
	 * private class callDeleteMerchant extends AsyncTask<String, Void,
	 * ArrayList<Long>> {
	 * 
	 * public callDeleteMerchant() { super(); }
	 * 
	 * @Override protected ArrayList<Long> doInBackground(String... params) { //
	 * The string name is query, providing that "isDelete" = 1 query =
	 * tbl_MerchantDao.Properties.IsDelete.columnName + " = '1'";
	 * ArrayList<tbl_Merchant> arrMerchant; ArrayList<Long> arrMerchantID = new
	 * ArrayList<Long>();
	 * 
	 * // get data in sqlite cursor = db.query(daoMerchant.getTablename(),
	 * daoMerchant.getAllColumns(), query, null, null, null, null); arrMerchant
	 * = new ArrayList<tbl_Merchant>();
	 * 
	 * // get ArrayList Merchant by cursor arrMerchant =
	 * tbl_Merchant.getObjectbyCursor(cursor); if (arrMerchant.size() > 0) { for
	 * (tbl_Merchant objMerchant : arrMerchant) { // get ID Merchant
	 * arrMerchantID.add(objMerchant.getMerchantID()); } // query server if
	 * (NetSupport.delete_receipt(arrMerchantID)) { return arrMerchantID; } }
	 * return null;
	 * 
	 * }
	 * 
	 * protected void onPostExecute(ArrayList<Long> arrMerchantID) { if
	 * (arrMerchantID != null) { } Message msg = Message.obtain(mHandler,
	 * Const.HANDLER_MERCHANT, Const.HANDLER_UPDATE, 0);
	 * mHandler.sendMessage(msg); } }
	 */

	/**
	 * call multiInsertMerchant
	 */
	private class callInsertMerchant extends
			AsyncTask<String, Void, ArrayList<tbl_Merchant>> {

		@Override
		protected ArrayList<tbl_Merchant> doInBackground(String... params) {
			// The string name is query, providing that "isDelete" = 1
			query = tbl_MerchantDao.Properties.IsCreate.columnName + " = '1'";
			ArrayList<tbl_Merchant> arrMerchant;

			// get data in sqlite
			cursor = db.query(daoMerchant.getTablename(),
					daoMerchant.getAllColumns(), query, null, null, null, null);
			arrMerchant = new ArrayList<tbl_Merchant>();

			// get ArrayList Merchant by cursor
			arrMerchant = tbl_Merchant.getObjectbyCursor(cursor);
			if (arrMerchant.size() > 0) {
				// query server
				if (NetSupport.insertMerchant(arrMerchant, context)) {
					return arrMerchant;
				}
			}
			return null;

		}

		protected void onPostExecute(ArrayList<tbl_Merchant> arrMerchantID) {
			updateMerchant();
		}
	}

	/**
	 * call update Merchant
	 */
	private class callUpdateMerchant extends
			AsyncTask<String, Void, ArrayList<tbl_Merchant>> {

		@Override
		protected ArrayList<tbl_Merchant> doInBackground(String... params) {
			// The string name is query, providing that "isDelete" = 1
			query = tbl_MerchantDao.Properties.IsUpdate.columnName + " = '1'";
			ArrayList<tbl_Merchant> arrMerchant;

			// get data in sqlite
			cursor = db.query(daoMerchant.getTablename(),
					daoMerchant.getAllColumns(), query, null, null, null, null);
			arrMerchant = new ArrayList<tbl_Merchant>();

			// get ArrayList Merchant by cursor
			arrMerchant = tbl_Merchant.getObjectbyCursor(cursor);
			if (arrMerchant.size() > 0) {

				// query server
				if (NetSupport.updateMerchant(arrMerchant, context)) {
					return arrMerchant;
				}
			}
			return null;

		}

		protected void onPostExecute(ArrayList<tbl_Merchant> arrMerchantID) {
			if (arrMerchantID != null) {

			}
		}
	}
}
